#!/bin/sh

for i in $(seq 1 90)
do
port1=$(netstat -nlp |grep ttnode|grep -Ev '127.0|tcp6|17331' |awk '!a[$4]++{print $4}' |sed 's/0.0.0.0://')
len1=${#port1}
if [ $len1 -gt 13 ]; then
	echo "成功获取端口!!"
	
	lan_dev=$(route | grep default | awk '{print $8}' | head -n 1)
	lan_ip=""
	if [ "$lan_dev" != "pppoe-wan" ];then
		lan_ip=$(ifconfig $lan_dev|grep "inet addr"|awk -F ":" '{print $2}'|awk '{print $1}')
	else
		lan_dev1=$(route | grep eth0 | awk '{print $8}' | head -n 1)
		lan_dev2=$(route | grep br-lan | awk '{print $8}' | head -n 1)
		if [ ${#lan_dev1} != 0 ]; then
			lan_ip=$(ifconfig $lan_dev1|grep "inet addr"|awk -F ":" '{print $2}'|awk '{print $1}')
			fi
		if [ ${#lan_dev2} != 0 ]; then
			lan_ip=$(ifconfig $lan_dev2|grep "inet addr"|awk -F ":" '{print $2}'|awk '{print $1}')
			fi
	fi
	
	echo -e "请将下面几个端口进行tcp和udp端口转发至ip：$lan_ip(转发前三个端口TCP和UDP)"
	echo -e $port1
	break
else
	echo "正在获取端口中..."
	if [ $i -eq 80 ]; then
		echo "获取端口失败..."
		echo "请重启容器..."
		rm -rf /mnts/.yfnode/config
		break
 	fi
 	sleep 60
fi
echo "循环第$i遍"
done
